<template>
    <div class="addEditLand">
        <Back :title="isEdit ? '编辑地址' : '新添地址'" />
        <div>
            <Bg>
                <van-address-edit
                    :area-list="areaList"
                    :show-delete="isEdit"
                    show-postal
                    show-set-default
                    :area-columns-placeholder="[
                        '选择省',
                        '选择市',
                        '选择区/县',
                    ]"
                    @save="onSave"
                    @delete="onDelete"
                    :address-info="addressData"
                />
            </Bg>
        </div>
    </div>
</template>

<script>
// 导入公共组件
import Back from "../components/Back.vue";
import Bg from "../components/Bg.vue";
import { Toast } from 'vant';

// 导入省市区的数据
import { areaList } from '@vant/area-data';

export default {
    name: "AddEditLand",
    components: {
        Back,
        Bg
    },

    data() {
        return {
            areaList,

            // 地址数据
            addressData: {},

            // 编辑状态
            isEdit: false,
        };
    },
    methods: {
        //  保存 新添/编辑 地址
        onSave(data) {
            
            // 调整参数
            delete data.country;


            // 判断是否编辑，进入判断是否修改
            if (this.isEdit) {
                // 修改开关
                let isModify = false;
                // 组件返回数据 与 初始请求数据 对比
                for (let key in data) {
                    if (data[key] !== this.addressData[key]) {
                        // 
                        isModify = true;

                        break;
                    }
                }

                // 判断是否 修改
                if (!isModify) {

                    // 没有修改就不请求，直接返回 地址列表
                    Toast('地址保存成功');

                    // 延迟跳转，观看提示
                    setTimeout(() => {
                        this.$router.back();
                    }, 1200);
                    return
                }
            }

            data.isDefault = data.isDefault ? 1 : 0;
            this.axios({
                method: "POST",
                url: this.isEdit ? "/editAddress" : "/addAddress",
                data

            }).then((res) => {
                
                if (res.data.code === 700) {
                    return this.$router.push({ name: "Logon" });
                }

                if (res.data.code === 9000) {
                    Toast('地址保存成功');

                    // 延迟跳转，观看提示
                    setTimeout(() => {
                        this.$router.back();
                    }, 1200);
                }
                if (res.data.code === 30000) {
                    Toast('地址编辑成功');
                    setTimeout(() => {
                        this.$router.back();
                    }, 1200);
                }
            }).catch((err) => {
                
            });
        },

        // 删除地址
        onDelete() {
            this.axios({
                method: "POST",
                url: "/deleteAddress",
                data: {
                    aid: this.addressData.aid
                }

            }).then((res) => {
                
                if (res.data.code === 700) {
                    return this.$router.push({ name: "Logon" });
                }

                if (res.data.code === 10000) {
                    Toast('地址删除成功');

                    setTimeout(() => {
                        this.$router.push({ name: "AddressList" });
                    }, 1200);

                }

            }).catch((err) => {
                
            });
            Toast('地址删除成功');
        },

        //  根据地址aid查询地址接口
        getFindAddressByAid(aid) {
            this.axios({
                method: "GET",
                url: '/findAddressByAid',
                params: { aid }

            }).then((res) => {
                
                if (res.data.code === 700) {
                    return this.$router.push({ name: "Logon" });
                }

                if (res.data.code === 40000) {
                    // 地址数据赋值
                    this.addressData = res.data.result[0];

                    this.addressData.isDefault = Boolean(this.addressData.isDefault);
                    
                }
            }).catch((err) => {
                
            });
        }
    },
    // 初始化钩子
    created() {
        // 获取 地址id
        let aid = this.$route.params.aid;
        // 判断 添加/编辑 模式
        if (aid) {
            this.isEdit = true;
            this.getFindAddressByAid(aid);
        }
        
    },
}

</script>

<style lang="less" scoped>
.addEditLand {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #f7f7f7;
    overflow: scroll;

    /deep/ .van-switch--on {
        background-color: #e99036;
        margin-left: 15px;
    }
}
</style>